<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActionView::Helpers::NumberHelper</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActionView::Helpers::NumberHelper</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/vendor/rails/actionpack/lib/action_view/helpers/number_helper_rb.html">
                vendor/rails/actionpack/lib/action_view/helpers/number_helper.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Provides methods for converting numbers into formatted strings. Methods are
provided for phone numbers, currency, percentage, precision, positional
notation, and file size.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M002059">number_to_currency</a>&nbsp;&nbsp;
      <a href="#M002063">number_to_human_size</a>&nbsp;&nbsp;
      <a href="#M002060">number_to_percentage</a>&nbsp;&nbsp;
      <a href="#M002058">number_to_phone</a>&nbsp;&nbsp;
      <a href="#M002061">number_with_delimiter</a>&nbsp;&nbsp;
      <a href="#M002062">number_with_precision</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M002059" class="method-detail">
        <a name="M002059"></a>

        <div class="method-heading">
          <a href="#M002059" class="method-signature">
          <span class="method-name">number_to_currency</span><span class="method-args">(number, options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Formats a <tt>number</tt> into a currency string (e.g., $13.65). You can
customize the format in the <tt>options</tt> hash.
</p>
<h4>Options</h4>
<ul>
<li><tt>:precision</tt> - Sets the level of precision (defaults to 2).

</li>
<li><tt>:unit</tt> - Sets the denomination of the currency (defaults to
&quot;$&quot;).

</li>
<li><tt>:separator</tt> - Sets the separator between the units (defaults to
&quot;.&quot;).

</li>
<li><tt>:delimiter</tt> - Sets the thousands delimiter (defaults to
&quot;,&quot;).

</li>
<li><tt>:format</tt> - Sets the format of the output string (defaults to
&quot;%u%n&quot;). The field types are:

<pre>
  %u  The currency unit
  %n  The number
</pre>
</li>
</ul>
<h4>Examples</h4>
<pre>
 number_to_currency(1234567890.50)                    # =&gt; $1,234,567,890.50
 number_to_currency(1234567890.506)                   # =&gt; $1,234,567,890.51
 number_to_currency(1234567890.506, :precision =&gt; 3)  # =&gt; $1,234,567,890.506

 number_to_currency(1234567890.50, :unit =&gt; &quot;&amp;pound;&quot;, :separator =&gt; &quot;,&quot;, :delimiter =&gt; &quot;&quot;)
 # =&gt; &amp;pound;1234567890,50
 number_to_currency(1234567890.50, :unit =&gt; &quot;&amp;pound;&quot;, :separator =&gt; &quot;,&quot;, :delimiter =&gt; &quot;&quot;, :format =&gt; &quot;%n %u&quot;)
 # =&gt; 1234567890,50 &amp;pound;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002059-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002059-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/number_helper.rb, line 71</span>
71:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">number_to_currency</span>(<span class="ruby-identifier">number</span>, <span class="ruby-identifier">options</span> = {})
72:         <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys!</span>
73: 
74:         <span class="ruby-identifier">defaults</span>  = <span class="ruby-constant">I18n</span>.<span class="ruby-identifier">translate</span>(<span class="ruby-value str">'number.format'</span><span class="ruby-value str">'number.format'</span>, <span class="ruby-identifier">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:locale</span>], <span class="ruby-identifier">:raise</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span>) <span class="ruby-keyword kw">rescue</span> {}
75:         <span class="ruby-identifier">currency</span>  = <span class="ruby-constant">I18n</span>.<span class="ruby-identifier">translate</span>(<span class="ruby-value str">'number.currency.format'</span><span class="ruby-value str">'number.currency.format'</span>, <span class="ruby-identifier">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:locale</span>], <span class="ruby-identifier">:raise</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span>) <span class="ruby-keyword kw">rescue</span> {}
76:         <span class="ruby-identifier">defaults</span>  = <span class="ruby-identifier">defaults</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">currency</span>)
77: 
78:         <span class="ruby-identifier">precision</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:precision</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:precision</span>]
79:         <span class="ruby-identifier">unit</span>      = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:unit</span>]      <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:unit</span>]
80:         <span class="ruby-identifier">separator</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:separator</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:separator</span>]
81:         <span class="ruby-identifier">delimiter</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:delimiter</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:delimiter</span>]
82:         <span class="ruby-identifier">format</span>    = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:format</span>]    <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:format</span>]
83:         <span class="ruby-identifier">separator</span> = <span class="ruby-value str">''</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">precision</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
84: 
85:         <span class="ruby-keyword kw">begin</span>
86:           <span class="ruby-identifier">format</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp re">/%n/</span>, <span class="ruby-identifier">number_with_precision</span>(<span class="ruby-identifier">number</span>,
87:             <span class="ruby-identifier">:precision</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">precision</span>,
88:             <span class="ruby-identifier">:delimiter</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">delimiter</span>,
89:             <span class="ruby-identifier">:separator</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">separator</span>)
90:           ).<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp re">/%u/</span>, <span class="ruby-identifier">unit</span>)
91:         <span class="ruby-keyword kw">rescue</span>
92:           <span class="ruby-identifier">number</span>
93:         <span class="ruby-keyword kw">end</span>
94:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M002063" class="method-detail">
        <a name="M002063"></a>

        <div class="method-heading">
          <a href="#M002063" class="method-signature">
          <span class="method-name">number_to_human_size</span><span class="method-args">(number, *args)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Formats the bytes in <tt>size</tt> into a more understandable
representation (e.g., giving it 1500 yields 1.5 KB). This method is useful
for reporting file sizes to users. This method returns nil if <tt>size</tt>
cannot be converted into a number. You can customize the format in the
<tt>options</tt> hash.
</p>
<h4>Options</h4>
<ul>
<li><tt>:precision</tt> - Sets the level of precision (defaults to 1).

</li>
<li><tt>:separator</tt> - Sets the separator between the units (defaults to
&quot;.&quot;).

</li>
<li><tt>:delimiter</tt> - Sets the thousands delimiter (defaults to
&quot;&quot;).

</li>
</ul>
<h4>Examples</h4>
<pre>
 number_to_human_size(123)                                          # =&gt; 123 Bytes
 number_to_human_size(1234)                                         # =&gt; 1.2 KB
 number_to_human_size(12345)                                        # =&gt; 12.1 KB
 number_to_human_size(1234567)                                      # =&gt; 1.2 MB
 number_to_human_size(1234567890)                                   # =&gt; 1.1 GB
 number_to_human_size(1234567890123)                                # =&gt; 1.1 TB
 number_to_human_size(1234567, :precision =&gt; 2)                     # =&gt; 1.18 MB
 number_to_human_size(483989, :precision =&gt; 0)                      # =&gt; 473 KB
 number_to_human_size(1234567, :precision =&gt; 2, :separator =&gt; ',')  # =&gt; 1,18 MB
</pre>
<p>
You can still use <tt><a
href="NumberHelper.html#M002063">number_to_human_size</a></tt> with the old
API that accepts the <tt>precision</tt> as its optional second parameter:
</p>
<pre>
 number_to_human_size(1234567, 2)    # =&gt; 1.18 MB
 number_to_human_size(483989, 0)     # =&gt; 473 KB
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002063-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002063-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/number_helper.rb, line 249</span>
249:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">number_to_human_size</span>(<span class="ruby-identifier">number</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
250:         <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">number</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-value">? </span><span class="ruby-keyword kw">nil</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">pluralize</span>(<span class="ruby-identifier">number</span>.<span class="ruby-identifier">to_i</span>, <span class="ruby-value str">&quot;Byte&quot;</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">number</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">1024</span>
251: 
252:         <span class="ruby-identifier">options</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">extract_options!</span>
253:         <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys!</span>
254: 
255:         <span class="ruby-identifier">defaults</span> = <span class="ruby-constant">I18n</span>.<span class="ruby-identifier">translate</span>(<span class="ruby-value str">'number.format'</span><span class="ruby-value str">'number.format'</span>, <span class="ruby-identifier">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:locale</span>], <span class="ruby-identifier">:raise</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span>) <span class="ruby-keyword kw">rescue</span> {}
256:         <span class="ruby-identifier">human</span>    = <span class="ruby-constant">I18n</span>.<span class="ruby-identifier">translate</span>(<span class="ruby-value str">'number.human.format'</span><span class="ruby-value str">'number.human.format'</span>, <span class="ruby-identifier">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:locale</span>], <span class="ruby-identifier">:raise</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span>) <span class="ruby-keyword kw">rescue</span> {}
257:         <span class="ruby-identifier">defaults</span> = <span class="ruby-identifier">defaults</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">human</span>)
258:         <span class="ruby-identifier">storage_units</span> = <span class="ruby-constant">I18n</span>.<span class="ruby-identifier">translate</span>(<span class="ruby-value str">'number.human.storage_units'</span><span class="ruby-value str">'number.human.storage_units'</span>, <span class="ruby-identifier">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:locale</span>], <span class="ruby-identifier">:raise</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span>)
259: 
260:         <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">empty?</span>
261:           <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Deprecation</span>.<span class="ruby-identifier">warn</span>(<span class="ruby-value str">'number_to_human_size takes an option hash '</span> <span class="ruby-operator">+</span>
262:             <span class="ruby-value str">'instead of a separate precision argument.'</span>, <span class="ruby-identifier">caller</span>)
263:           <span class="ruby-identifier">precision</span> = <span class="ruby-identifier">args</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:precision</span>]
264:         <span class="ruby-keyword kw">end</span>
265: 
266:         <span class="ruby-identifier">precision</span> <span class="ruby-operator">||=</span> (<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:precision</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:precision</span>])
267:         <span class="ruby-identifier">separator</span> <span class="ruby-operator">||=</span> (<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:separator</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:separator</span>])
268:         <span class="ruby-identifier">delimiter</span> <span class="ruby-operator">||=</span> (<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:delimiter</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:delimiter</span>])
269: 
270:         <span class="ruby-identifier">max_exp</span>  = <span class="ruby-identifier">storage_units</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>
271:         <span class="ruby-identifier">number</span>   = <span class="ruby-constant">Float</span>(<span class="ruby-identifier">number</span>)
272:         <span class="ruby-identifier">exponent</span> = (<span class="ruby-constant">Math</span>.<span class="ruby-identifier">log</span>(<span class="ruby-identifier">number</span>) <span class="ruby-operator">/</span> <span class="ruby-constant">Math</span>.<span class="ruby-identifier">log</span>(<span class="ruby-value">1024</span>)).<span class="ruby-identifier">to_i</span> <span class="ruby-comment cmt"># Convert to base 1024</span>
273:         <span class="ruby-identifier">exponent</span> = <span class="ruby-identifier">max_exp</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">exponent</span> <span class="ruby-operator">&gt;</span> <span class="ruby-identifier">max_exp</span> <span class="ruby-comment cmt"># we need this to avoid overflow for the highest unit</span>
274:         <span class="ruby-identifier">number</span>  <span class="ruby-operator">/=</span> <span class="ruby-value">1024</span> <span class="ruby-operator">**</span> <span class="ruby-identifier">exponent</span>
275:         <span class="ruby-identifier">unit</span>     = <span class="ruby-identifier">storage_units</span>[<span class="ruby-identifier">exponent</span>]
276: 
277:         <span class="ruby-keyword kw">begin</span>
278:           <span class="ruby-identifier">escaped_separator</span> = <span class="ruby-constant">Regexp</span>.<span class="ruby-identifier">escape</span>(<span class="ruby-identifier">separator</span>)
279:           <span class="ruby-identifier">number_with_precision</span>(<span class="ruby-identifier">number</span>,
280:             <span class="ruby-identifier">:precision</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">precision</span>,
281:             <span class="ruby-identifier">:separator</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">separator</span>,
282:             <span class="ruby-identifier">:delimiter</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">delimiter</span>
283:           ).<span class="ruby-identifier">sub</span>(<span class="ruby-node">/(\d)(#{escaped_separator}[1-9]*)?0+\z/</span>, <span class="ruby-value str">'\1\2'</span>).<span class="ruby-identifier">sub</span>(<span class="ruby-node">/#{escaped_separator}\z/</span>, <span class="ruby-value str">''</span>) <span class="ruby-operator">+</span> <span class="ruby-node">&quot; #{unit}&quot;</span>
284:         <span class="ruby-keyword kw">rescue</span>
285:           <span class="ruby-identifier">number</span>
286:         <span class="ruby-keyword kw">end</span>
287:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M002060" class="method-detail">
        <a name="M002060"></a>

        <div class="method-heading">
          <a href="#M002060" class="method-signature">
          <span class="method-name">number_to_percentage</span><span class="method-args">(number, options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Formats a <tt>number</tt> as a percentage string (e.g., 65%). You can
customize the format in the <tt>options</tt> hash.
</p>
<h4>Options</h4>
<ul>
<li><tt>:precision</tt> - Sets the level of precision (defaults to 3).

</li>
<li><tt>:separator</tt> - Sets the separator between the units (defaults to
&quot;.&quot;).

</li>
<li><tt>:delimiter</tt> - Sets the thousands delimiter (defaults to
&quot;&quot;).

</li>
</ul>
<h4>Examples</h4>
<pre>
 number_to_percentage(100)                                        # =&gt; 100.000%
 number_to_percentage(100, :precision =&gt; 0)                       # =&gt; 100%
 number_to_percentage(1000, :delimiter =&gt; '.', :separator =&gt; ',') # =&gt; 1.000,000%
 number_to_percentage(302.24398923423, :precision =&gt; 5)           # =&gt; 302.24399%
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002060-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002060-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/number_helper.rb, line 109</span>
109:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">number_to_percentage</span>(<span class="ruby-identifier">number</span>, <span class="ruby-identifier">options</span> = {})
110:         <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys!</span>
111: 
112:         <span class="ruby-identifier">defaults</span>   = <span class="ruby-constant">I18n</span>.<span class="ruby-identifier">translate</span>(<span class="ruby-value str">'number.format'</span><span class="ruby-value str">'number.format'</span>, <span class="ruby-identifier">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:locale</span>], <span class="ruby-identifier">:raise</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span>) <span class="ruby-keyword kw">rescue</span> {}
113:         <span class="ruby-identifier">percentage</span> = <span class="ruby-constant">I18n</span>.<span class="ruby-identifier">translate</span>(<span class="ruby-value str">'number.percentage.format'</span><span class="ruby-value str">'number.percentage.format'</span>, <span class="ruby-identifier">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:locale</span>], <span class="ruby-identifier">:raise</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span>) <span class="ruby-keyword kw">rescue</span> {}
114:         <span class="ruby-identifier">defaults</span>  = <span class="ruby-identifier">defaults</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">percentage</span>)
115: 
116:         <span class="ruby-identifier">precision</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:precision</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:precision</span>]
117:         <span class="ruby-identifier">separator</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:separator</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:separator</span>]
118:         <span class="ruby-identifier">delimiter</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:delimiter</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:delimiter</span>]
119: 
120:         <span class="ruby-keyword kw">begin</span>
121:           <span class="ruby-identifier">number_with_precision</span>(<span class="ruby-identifier">number</span>,
122:             <span class="ruby-identifier">:precision</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">precision</span>,
123:             <span class="ruby-identifier">:separator</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">separator</span>,
124:             <span class="ruby-identifier">:delimiter</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">delimiter</span>) <span class="ruby-operator">+</span> <span class="ruby-value str">&quot;%&quot;</span>
125:         <span class="ruby-keyword kw">rescue</span>
126:           <span class="ruby-identifier">number</span>
127:         <span class="ruby-keyword kw">end</span>
128:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M002058" class="method-detail">
        <a name="M002058"></a>

        <div class="method-heading">
          <a href="#M002058" class="method-signature">
          <span class="method-name">number_to_phone</span><span class="method-args">(number, options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Formats a <tt>number</tt> into a US phone number (e.g., (555) 123-9876).
You can customize the format in the <tt>options</tt> hash.
</p>
<h4>Options</h4>
<ul>
<li><tt>:area_code</tt> - Adds parentheses around the area code.

</li>
<li><tt>:delimiter</tt> - Specifies the delimiter to use (defaults to
&quot;-&quot;).

</li>
<li><tt>:extension</tt> - Specifies an extension to add to the end of the
generated number.

</li>
<li><tt>:country_code</tt> - Sets the country code for the phone number.

</li>
</ul>
<h4>Examples</h4>
<pre>
 number_to_phone(1235551234)                                        # =&gt; 123-555-1234
 number_to_phone(1235551234, :area_code =&gt; true)                    # =&gt; (123) 555-1234
 number_to_phone(1235551234, :delimiter =&gt; &quot; &quot;)                     # =&gt; 123 555 1234
 number_to_phone(1235551234, :area_code =&gt; true, :extension =&gt; 555) # =&gt; (123) 555-1234 x 555
 number_to_phone(1235551234, :country_code =&gt; 1)                    # =&gt; +1-123-555-1234

 number_to_phone(1235551234, :country_code =&gt; 1, :extension =&gt; 1343, :delimiter =&gt; &quot;.&quot;)
 =&gt; +1.123.555.1234 x 1343
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002058-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002058-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/number_helper.rb, line 26</span>
26:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">number_to_phone</span>(<span class="ruby-identifier">number</span>, <span class="ruby-identifier">options</span> = {})
27:         <span class="ruby-identifier">number</span>       = <span class="ruby-identifier">number</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">strip</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">number</span>.<span class="ruby-identifier">nil?</span>
28:         <span class="ruby-identifier">options</span>      = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys</span>
29:         <span class="ruby-identifier">area_code</span>    = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:area_code</span>] <span class="ruby-operator">||</span> <span class="ruby-keyword kw">nil</span>
30:         <span class="ruby-identifier">delimiter</span>    = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:delimiter</span>] <span class="ruby-operator">||</span> <span class="ruby-value str">&quot;-&quot;</span>
31:         <span class="ruby-identifier">extension</span>    = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:extension</span>].<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">strip</span> <span class="ruby-operator">||</span> <span class="ruby-keyword kw">nil</span>
32:         <span class="ruby-identifier">country_code</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:country_code</span>] <span class="ruby-operator">||</span> <span class="ruby-keyword kw">nil</span>
33: 
34:         <span class="ruby-keyword kw">begin</span>
35:           <span class="ruby-identifier">str</span> = <span class="ruby-value str">&quot;&quot;</span>
36:           <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-node">&quot;+#{country_code}#{delimiter}&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">country_code</span>.<span class="ruby-identifier">blank?</span>
37:           <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">area_code</span>
38:             <span class="ruby-identifier">number</span>.<span class="ruby-identifier">gsub!</span>(<span class="ruby-regexp re">/([0-9]{1,3})([0-9]{3})([0-9]{4}$)/</span>,<span class="ruby-node">&quot;(\\1) \\2#{delimiter}\\3&quot;</span>)
39:           <span class="ruby-keyword kw">else</span>
40:             <span class="ruby-identifier">number</span>.<span class="ruby-identifier">gsub!</span>(<span class="ruby-regexp re">/([0-9]{1,3})([0-9]{3})([0-9]{4})$/</span>,<span class="ruby-node">&quot;\\1#{delimiter}\\2#{delimiter}\\3&quot;</span>)
41:           <span class="ruby-keyword kw">end</span>
42:           <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-node">&quot; x #{extension}&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">extension</span>.<span class="ruby-identifier">blank?</span>
43:           <span class="ruby-identifier">str</span>
44:         <span class="ruby-keyword kw">rescue</span>
45:           <span class="ruby-identifier">number</span>
46:         <span class="ruby-keyword kw">end</span>
47:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M002061" class="method-detail">
        <a name="M002061"></a>

        <div class="method-heading">
          <a href="#M002061" class="method-signature">
          <span class="method-name">number_with_delimiter</span><span class="method-args">(number, *args)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Formats a <tt>number</tt> with grouped thousands using <tt>delimiter</tt>
(e.g., 12,324). You can customize the format in the <tt>options</tt> hash.
</p>
<h4>Options</h4>
<ul>
<li><tt>:delimiter</tt> - Sets the thousands delimiter (defaults to
&quot;,&quot;).

</li>
<li><tt>:separator</tt> - Sets the separator between the units (defaults to
&quot;.&quot;).

</li>
</ul>
<h4>Examples</h4>
<pre>
 number_with_delimiter(12345678)                        # =&gt; 12,345,678
 number_with_delimiter(12345678.05)                     # =&gt; 12,345,678.05
 number_with_delimiter(12345678, :delimiter =&gt; &quot;.&quot;)     # =&gt; 12.345.678
 number_with_delimiter(12345678, :seperator =&gt; &quot;,&quot;)     # =&gt; 12,345,678
 number_with_delimiter(98765432.98, :delimiter =&gt; &quot; &quot;, :separator =&gt; &quot;,&quot;)
 # =&gt; 98 765 432,98
</pre>
<p>
You can still use <tt><a
href="NumberHelper.html#M002061">number_with_delimiter</a></tt> with the
old API that accepts the <tt>delimiter</tt> as its optional second and the
<tt>separator</tt> as its optional third parameter:
</p>
<pre>
 number_with_delimiter(12345678, &quot; &quot;)                     # =&gt; 12 345.678
 number_with_delimiter(12345678.05, &quot;.&quot;, &quot;,&quot;)             # =&gt; 12.345.678,05
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002061-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002061-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/number_helper.rb, line 150</span>
150:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">number_with_delimiter</span>(<span class="ruby-identifier">number</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
151:         <span class="ruby-identifier">options</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">extract_options!</span>
152:         <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys!</span>
153: 
154:         <span class="ruby-identifier">defaults</span> = <span class="ruby-constant">I18n</span>.<span class="ruby-identifier">translate</span>(<span class="ruby-value str">'number.format'</span><span class="ruby-value str">'number.format'</span>, <span class="ruby-identifier">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:locale</span>], <span class="ruby-identifier">:raise</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span>) <span class="ruby-keyword kw">rescue</span> {}
155: 
156:         <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">empty?</span>
157:           <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Deprecation</span>.<span class="ruby-identifier">warn</span>(<span class="ruby-value str">'number_with_delimiter takes an option hash '</span> <span class="ruby-operator">+</span>
158:             <span class="ruby-value str">'instead of separate delimiter and precision arguments.'</span>, <span class="ruby-identifier">caller</span>)
159:           <span class="ruby-identifier">delimiter</span> = <span class="ruby-identifier">args</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:delimiter</span>]
160:           <span class="ruby-identifier">separator</span> = <span class="ruby-identifier">args</span>[<span class="ruby-value">1</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:separator</span>]
161:         <span class="ruby-keyword kw">end</span>
162: 
163:         <span class="ruby-identifier">delimiter</span> <span class="ruby-operator">||=</span> (<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:delimiter</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:delimiter</span>])
164:         <span class="ruby-identifier">separator</span> <span class="ruby-operator">||=</span> (<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:separator</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:separator</span>])
165: 
166:         <span class="ruby-keyword kw">begin</span>
167:           <span class="ruby-identifier">parts</span> = <span class="ruby-identifier">number</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">split</span>(<span class="ruby-value str">'.'</span>)
168:           <span class="ruby-identifier">parts</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">gsub!</span>(<span class="ruby-regexp re">/(\d)(?=(\d\d\d)+(?!\d))/</span>, <span class="ruby-node">&quot;\\1#{delimiter}&quot;</span>)
169:           <span class="ruby-identifier">parts</span>.<span class="ruby-identifier">join</span>(<span class="ruby-identifier">separator</span>)
170:         <span class="ruby-keyword kw">rescue</span>
171:           <span class="ruby-identifier">number</span>
172:         <span class="ruby-keyword kw">end</span>
173:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M002062" class="method-detail">
        <a name="M002062"></a>

        <div class="method-heading">
          <a href="#M002062" class="method-signature">
          <span class="method-name">number_with_precision</span><span class="method-args">(number, *args)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Formats a <tt>number</tt> with the specified level of <tt>:precision</tt>
(e.g., 112.32 has a precision of 2). You can customize the format in the
<tt>options</tt> hash.
</p>
<h4>Options</h4>
<ul>
<li><tt>:precision</tt> - Sets the level of precision (defaults to 3).

</li>
<li><tt>:separator</tt> - Sets the separator between the units (defaults to
&quot;.&quot;).

</li>
<li><tt>:delimiter</tt> - Sets the thousands delimiter (defaults to
&quot;&quot;).

</li>
</ul>
<h4>Examples</h4>
<pre>
 number_with_precision(111.2345)                    # =&gt; 111.235
 number_with_precision(111.2345, :precision =&gt; 2)   # =&gt; 111.23
 number_with_precision(13, :precision =&gt; 5)         # =&gt; 13.00000
 number_with_precision(389.32314, :precision =&gt; 0)  # =&gt; 389
 number_with_precision(1111.2345, :precision =&gt; 2, :separator =&gt; ',', :delimiter =&gt; '.')
 # =&gt; 1.111,23
</pre>
<p>
You can still use <tt><a
href="NumberHelper.html#M002062">number_with_precision</a></tt> with the
old API that accepts the <tt>precision</tt> as its optional second
parameter:
</p>
<pre>
  number_with_precision(number_with_precision(111.2345, 2)   # =&gt; 111.23
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002062-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002062-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/number_helper.rb, line 194</span>
194:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">number_with_precision</span>(<span class="ruby-identifier">number</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
195:         <span class="ruby-identifier">options</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">extract_options!</span>
196:         <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys!</span>
197: 
198:         <span class="ruby-identifier">defaults</span>           = <span class="ruby-constant">I18n</span>.<span class="ruby-identifier">translate</span>(<span class="ruby-value str">'number.format'</span><span class="ruby-value str">'number.format'</span>, <span class="ruby-identifier">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:locale</span>], <span class="ruby-identifier">:raise</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span>) <span class="ruby-keyword kw">rescue</span> {}
199:         <span class="ruby-identifier">precision_defaults</span> = <span class="ruby-constant">I18n</span>.<span class="ruby-identifier">translate</span>(<span class="ruby-value str">'number.precision.format'</span><span class="ruby-value str">'number.precision.format'</span>, <span class="ruby-identifier">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:locale</span>],
200:                                                                         <span class="ruby-identifier">:raise</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span>) <span class="ruby-keyword kw">rescue</span> {}
201:         <span class="ruby-identifier">defaults</span>           = <span class="ruby-identifier">defaults</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">precision_defaults</span>)
202: 
203:         <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">empty?</span>
204:           <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Deprecation</span>.<span class="ruby-identifier">warn</span>(<span class="ruby-value str">'number_with_precision takes an option hash '</span> <span class="ruby-operator">+</span>
205:             <span class="ruby-value str">'instead of a separate precision argument.'</span>, <span class="ruby-identifier">caller</span>)
206:           <span class="ruby-identifier">precision</span> = <span class="ruby-identifier">args</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:precision</span>]
207:         <span class="ruby-keyword kw">end</span>
208: 
209:         <span class="ruby-identifier">precision</span> <span class="ruby-operator">||=</span> (<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:precision</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:precision</span>])
210:         <span class="ruby-identifier">separator</span> <span class="ruby-operator">||=</span> (<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:separator</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:separator</span>])
211:         <span class="ruby-identifier">delimiter</span> <span class="ruby-operator">||=</span> (<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:delimiter</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">defaults</span>[<span class="ruby-identifier">:delimiter</span>])
212: 
213:         <span class="ruby-keyword kw">begin</span>
214:           <span class="ruby-identifier">rounded_number</span> = (<span class="ruby-constant">Float</span>(<span class="ruby-identifier">number</span>) <span class="ruby-operator">*</span> (<span class="ruby-value">10</span> <span class="ruby-operator">**</span> <span class="ruby-identifier">precision</span>)).<span class="ruby-identifier">round</span>.<span class="ruby-identifier">to_f</span> <span class="ruby-operator">/</span> <span class="ruby-value">10</span> <span class="ruby-operator">**</span> <span class="ruby-identifier">precision</span>
215:           <span class="ruby-identifier">number_with_delimiter</span>(<span class="ruby-node">&quot;%01.#{precision}f&quot;</span> <span class="ruby-operator">%</span> <span class="ruby-identifier">rounded_number</span>,
216:             <span class="ruby-identifier">:separator</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">separator</span>,
217:             <span class="ruby-identifier">:delimiter</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">delimiter</span>)
218:         <span class="ruby-keyword kw">rescue</span>
219:           <span class="ruby-identifier">number</span>
220:         <span class="ruby-keyword kw">end</span>
221:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>